package com.jk.hly.service.impl;

import com.github.pagehelper.PageHelper;
import com.jk.hly.bean.HlyUser;
import com.jk.hly.bean.Tree;
import com.jk.hly.mapper.HlyTreeMapper;
import com.jk.hly.service.HlyTreeService;
import com.jk.hly.util.ReceivePage;
import com.jk.hly.util.SendPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HlyTreeServiceImpl implements HlyTreeService {

    @Resource
    private HlyTreeMapper   treeMapper;
    @Override
    public List<Tree> queryTree() {
        List<Tree> treeList=treeMapper.queryTree(0);
        queryTreeList(treeList);
        return treeList;
    }

    @Override
    public SendPage queryUserPhone(ReceivePage page) {
        List<HlyUser> list=treeMapper.queryUserPhone();
        PageHelper.startPage(page.getPage(), page.getRows());
        List<HlyUser> data=treeMapper.queryUserPhone();
        SendPage sp=new SendPage(list.size(), data);
        return sp;
    }

    @Override
    public void deleteUserPhone(String ids) {
        treeMapper.deleteUserPhone(ids);
    }

    @Override
    public HlyUser queryById(String id) {
        HlyUser  us=treeMapper.queryById(id);
        return us;
    }

    @Override
    public void updateUserInfo(HlyUser user) {
        treeMapper.updateUserInfo(user);
    }

    @Override
    public void addUserPhone(HlyUser user) {
        treeMapper.addUserPhone(user);
    }

    @Override
    public List<HlyUser> queryAllUser(String ids) {
        return treeMapper.queryAllUser(ids);
    }

    @Override
    public void insertPhone(HlyUser u) {
        treeMapper.addUserPhone(u);
    }

    @Override
    public HlyUser queryPhone(String phone) {

        return treeMapper.queryPhone(phone);
    }

    private void queryTreeList(List<Tree> treeList) {
        for (Tree tree : treeList) {
            List<Tree> treeChildren=treeMapper.queryTree(tree.getId());
            queryTreeList(treeChildren);
            if(treeChildren.size()>0)
            tree.setNodes(treeChildren);
        }
    }
}
